﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using Intermediario;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Channels;

using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Collections;

namespace ClienteNotaria
{
    public partial class Form1 : Form
    {
        ClienteDeNotaria mClienteNotaria;
        IServidorPrincipal mServidor;
        Cita obj;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                mClienteNotaria = new ClienteDeNotaria(this); //Enlace con "ClienteDeChat"

                //mClienteNotaria.MostrarMensaje += new ClienteDeChat.DelegadoMostrarMensaje(VeElMensaje); //5

                //1. Leer la configuración del 'App.config' (Se configura el Remoting)
                RemotingConfiguration.Configure("ClienteNotaria.exe.config", false);
                //2. Obtener uno de los servicios registrados en 'App.config' (<client> <wellknown type =)
                foreach (WellKnownClientTypeEntry entr in RemotingConfiguration.GetRegisteredWellKnownClientTypes())
                {
                    if (entr.ObjectType != null)
                    {
                        //3. Activa un objeto de la URL Configurada
                        mServidor = (Intermediario.IServidorPrincipal)Activator.GetObject(entr.ObjectType, entr.ObjectUrl);
                    }
                }

                mClienteNotaria.Nombre = "Notaria Segunda de Popayán";
                Thread MyHilo = new Thread(new ThreadStart(Conectar));
                MyHilo.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
                    
        }

        public void Solicitantes()
        {
            ArrayList listObj = this.mServidor.ObtenerSolictantes(6);
            dataGridView1.DataSource = listObj;
        }

        public void Asignar() 
        {
            obj.FechaCita = Convert.ToDateTime(dtmFecha.Text);
            this.mServidor.AsignarCita(this.mClienteNotaria,obj);
        }

        public void Conectar()
        {
            this.mServidor.Conectar(this.mClienteNotaria, this.mClienteNotaria.Nombre, 6);

            //Esto modifica un control de Interfaz [No debe hacerse]
            //La interfaz tiene su propio hilo de ejecución
            this.lblConectado.Text = "Conectado.";
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                if (MessageBox.Show("Desea desconectarse del Servidor?", "Cliente Notaria", MessageBoxButtons.YesNo) == DialogResult.No)
                {
                    // Cancel the Closing event from closing the form.
                    e.Cancel = true;
                    // Call method to save file...
                }
                else
                {
                    this.mServidor.Desconectar(this.mClienteNotaria, this.mClienteNotaria.Nombre);
                    //Application.Exit();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
                         
        }

        private void btnCargar_Click(object sender, EventArgs e)
        {
            try
            {
                //System.Collections.ArrayList listObj = this.mServidor.ObtenerSolictantes(1);
                //MessageBox.Show(listObj.Count.ToString());
                Thread MyHilo = new Thread(new ThreadStart(Solicitantes));
                MyHilo.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {                
                txtSolicitante.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[1].Value);

                string nombreNotaria = Convert.ToString(this.dataGridView1.CurrentRow.Cells[2].Value);
                string idNotaria = Convert.ToString(this.dataGridView1.CurrentRow.Cells[5].Value);
                Dictionary<string, string> test = new Dictionary<string, string>();
                test.Add(idNotaria, nombreNotaria);
                cmbNotaria.DataSource = new BindingSource(test, null);
                cmbNotaria.DisplayMember = "Value";
                cmbNotaria.ValueMember = "Key";
                //string value = ((KeyValuePair<string, string>)comboBox1.SelectedItem).Value;

                string nombreTramite = Convert.ToString(this.dataGridView1.CurrentRow.Cells[3].Value);
                string idTramite = Convert.ToString(this.dataGridView1.CurrentRow.Cells[8].Value);
                test = new Dictionary<string, string>();
                test.Add(idTramite, nombreTramite);
                cmbTramite.DataSource = new BindingSource(test, null);
                cmbTramite.DisplayMember = "Value";
                cmbTramite.ValueMember = "Key";

                txtDocumentos.Text = Convert.ToString(this.dataGridView1.CurrentRow.Cells[15].Value);

                obj = new Cita();
                obj.NombreSol = txtSolicitante.Text;
                obj.IdNotaria =Convert.ToInt32( idNotaria);
                obj.NombreNota = nombreNotaria;
                obj.TipoTramite = Convert.ToInt32( idTramite);
                obj.NombreTramite = nombreTramite;
                obj.DocsAnexos = txtDocumentos.Text;
                obj.IdCita = Convert.ToInt32(this.dataGridView1.CurrentRow.Cells[0].Value);
                obj.IdAsignador = 9;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }            
        }
        public void BandejaFuncionario()
        {
            ArrayList listObj = this.mServidor.ObtenerBandejaFuncionario(5, 2);
            dataGridView2.DataSource = listObj;
        }

        private void btnAsignar_Click(object sender, EventArgs e)
        {
            try
            {
                Thread MyHilo = new Thread(new ThreadStart(Asignar));
                MyHilo.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }  
        }

        private void btnFirmar_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                Thread MyHilo = new Thread(new ThreadStart(BandejaFuncionario));
                MyHilo.Start();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }


    }
}
